package com.lanou.lenanapp.adapter;

import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;
import com.lanou.lenanapp.R;
import com.lanou.lenanapp.beans.Special;
import com.lanou.lenanapp.volley.VolleySingleton;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by dllo on 15/10/28.
 */
public class SpecialAdapter extends BaseAdapter{

    private TextView special_aname, special_time, special_like;
    private ImageView special_aicon;
    private List<Special> specials;
    VolleySingleton volleySingleton = VolleySingleton.getInstance();



    public void addData(List<Special> datas){
        this.specials = datas;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return specials != null && specials.size() > 0 ? specials.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return specials.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater inflater = LayoutInflater.from(parent.getContext());
            convertView = inflater.inflate(R.layout.activity_special_gut, null);
        }
        special_aname = (TextView) convertView.findViewById(R.id.special_aname);
        special_like = (TextView) convertView.findViewById(R.id.special_like);
        special_time = (TextView) convertView.findViewById(R.id.special_time);
        special_aicon = (ImageView) convertView.findViewById(R.id.special_aicon);


        if (specials != null && specials.size() > 0) {
            Special special = specials.get(position);
            //标题
            special_aname.setText(special.getAname());
            //赞的数量
            double d = Double.parseDouble(special.getLikes());
            int t = (int) d;
            special_like.setText(t + "");
            //发布时间
            special_time.setText(getNormalTime(special.getUpdate_time()));
            //设置封面图片
            String url = special.getAicon();
            getBitMap(url, special_aicon);

        }
        return convertView;
    }



    private void getBitMap(String url, final ImageView imageView) {
        ImageRequest request = new ImageRequest(url, new Response.Listener<Bitmap>() {
            @Override
            public void onResponse(Bitmap response) {
                imageView.setImageBitmap(response);
            }
        }, 1000, 1000, Bitmap.Config.ARGB_8888, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        });

        volleySingleton.addRequest(request);
    }


    /**
     * 时间格式转换方法
     * **/
    public  String  getNormalTime( String value) {
        Long l = Long.valueOf(value);
        l = l * 1000;
        Date date = new Date(l);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") ;
        String time = format.format(date) ;
        return time;
    }



}
